package com.syys.qa.controller;

import java.util.List;

import com.syys.common.utils.MessageUtils;
import com.syys.qa.util.IpUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.syys.common.annotation.Log;
import com.syys.common.enums.BusinessType;
import com.syys.qa.domain.QaRecord;
import com.syys.qa.service.IQaRecordService;
import com.syys.common.core.controller.BaseController;
import com.syys.common.core.domain.AjaxResult;
import com.syys.common.utils.poi.ExcelUtil;
import com.syys.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 问卷记录Controller
 *
 * @author ys
 * @date 2021-07-01
 */
@Controller
@RequestMapping("/qa/record")
public class QaRecordController extends BaseController {
    private String prefix = "qa/record";

    @Autowired
    private IQaRecordService qaRecordService;

    @RequiresPermissions("qa:record:view")
    @GetMapping()
    public String record(String themeId, ModelMap mmap) {
        mmap.put("themeId", themeId);
        return prefix + "/record";
    }

    /**
     * 查询问卷记录列表
     */
    @RequiresPermissions("qa:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QaRecord qaRecord) {
        startPage();
        List<QaRecord> list = qaRecordService.selectQaRecordList(qaRecord);
        return getDataTable(list);
    }

    /**
     * 导出问卷记录列表
     */
    @RequiresPermissions("qa:record:export")
    @Log(title = "问卷记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QaRecord qaRecord) {
        List<QaRecord> list = qaRecordService.selectQaRecordList(qaRecord);
        ExcelUtil<QaRecord> util = new ExcelUtil<QaRecord>(QaRecord.class);
        return util.exportExcel(list, "问卷记录数据");
    }

    /**
     * 新增问卷记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存问卷记录
     */
//    @RequiresPermissions("qa:record:add")
    @Log(title = "问卷记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QaRecord qaRecord, HttpServletRequest request) {
        String ipAddress = IpUtil.getIpAddress(request);
        qaRecord.setIp(ipAddress);
        int i = qaRecordService.insertQaRecord(qaRecord);
        if (i > 0) {
            return AjaxResult.success(qaRecord.getRecordId());
        } else {
            return AjaxResult.error(MessageUtils.message("qa.create.error"));
        }
    }

    /**
     * 修改问卷记录
     */
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        QaRecord qaRecord = qaRecordService.selectQaRecordById(recordId);
        mmap.put("qaRecord", qaRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存问卷记录
     */
    @RequiresPermissions("qa:record:edit")
    @Log(title = "问卷记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QaRecord qaRecord) {
        return toAjax(qaRecordService.updateQaRecord(qaRecord));
    }

    /**
     * 删除问卷记录
     */
    @RequiresPermissions("qa:record:remove")
    @Log(title = "问卷记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(qaRecordService.deleteQaRecordByIds(ids));
    }
}
